package my.wudm.yummy.Service.impl;

import cn.zyzpp.AiPa;
import cn.zyzpp.executor.AiPaExecutor;
import my.wudm.yummy.Service.IBiquService;
import my.wudm.yummy.common.model.BaseCatalogModel;
import my.wudm.yummy.common.model.BaseContentModel;
import my.wudm.yummy.worker.WebWorker;
import org.springframework.stereotype.Service;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

@Service
public class BiquServiceImpl implements IBiquService {

    @Override
    public BaseCatalogModel getCatalog(String url) {
        boolean isParseCatalog = true;
        List<String> linkList = new ArrayList<>();
        BaseCatalogModel model = null;

        linkList.add(url);
        AiPaExecutor aiPaExecutor = AiPa.newInstance(new WebWorker(isParseCatalog)).setCharset(Charset.forName("GBK"));
        aiPaExecutor.submit(linkList);
        List<Future> futureList = aiPaExecutor.getFutureList();
        for (int i = 0; i < futureList.size(); i++) {
            try {
                model = (BaseCatalogModel) futureList.get(i).get();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }
        aiPaExecutor.shutdown();
        return model;
    }

    @Override
    public BaseContentModel getContent(String url) {
        return null;
    }
}
